package com.imooc.countdown;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

public class CheckStartUp {
	private static List<DangerCerter> stationList;
	private static CountDownLatch countDown;

	public CheckStartUp() {

	}

	public static boolean checkAllStations() throws InterruptedException {

		// 初始化调度站
		countDown = new CountDownLatch(3);

		// 把所有站点添加进list
		stationList = new ArrayList<>();
		stationList.add(new StationBeJingIMooc(countDown));
		stationList.add(new StationJiangsuSanling(countDown));

		// 使用线程池
		Executor executor = Executors.newFixedThreadPool(stationList.size());
		for (DangerCerter dangerCerter : stationList) {
			executor.execute(dangerCerter);
		}

		// 等待线程执行完毕
		countDown.await();

		for (DangerCerter center : stationList) {
			if (!center.isOk()) {
				return false;
			}
		}

		return true;

	}

	public static List<DangerCerter> getStationList() {
		return stationList;
	}

	public static void setStationList(List<DangerCerter> stationList) {
		CheckStartUp.stationList = stationList;
	}

	public static CountDownLatch getCountDown() {
		return countDown;
	}

	public static void setCountDown(CountDownLatch countDown) {
		CheckStartUp.countDown = countDown;
	}

}
